import { lazy } from 'react';
import { utils } from '@ohif/core';

const RimagViewerRouting = utils.asyncComponent(lazy(() => import('./RimagViewerRouting.js')));
const ViewerLocalFileData = utils.asyncComponent(lazy(() => import('../components/ViewerLocalFileData')));

const reload = () => window.location.reload();

const ROUTES_DEF = {
  default: {
    rimag: {
      path: '/vw/:accessKey',
      component: RimagViewerRouting,
    },
    local: {
      path: '/local',
      component: ViewerLocalFileData,
    },
  },
};

const getRoutes = appConfig => {
  const routes = [];
  for (let keyConfig in ROUTES_DEF) {
    const routesConfig = ROUTES_DEF[keyConfig];

    for (let routeKey in routesConfig) {
      const route = routesConfig[routeKey];
      const validRoute =
        typeof route.condition === 'function'
          ? route.condition(appConfig)
          : true;

      if (validRoute) {
        routes.push({
          path: route.path,
          Component: route.component,
        });
      }
    }
  }

  return routes;
};

export { getRoutes, reload };
